#导包
from bs4 import BeautifulSoup
import requests
from openpyxl import Workbook
from pyecharts.charts import Line,Bar,Pie,Grid
from pyecharts import options

#封装数据（城市，房价）
money=[]
city=[]
rate=[]

#封装头部信息
header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36",
    "Cookie": "_csrfToken=DOM5Wxba4QTc8ZCtYsu0baNPqubmINBcLdATiYkV; newstatisticUUID=1622912574_1888313421"
}

# 爬取数据
url = "http://www.cityhouse.cn/default/forsalerank.html"
html = requests.get(url, headers=header)

# 解析数据提取（城市，房价）
soup = BeautifulSoup(html.text,'html.parser')
for i in range(1,12):
    soups = soup.select("#order_f > tr:nth-child({}) > td:nth-child(3) > span".format(i))
    p = soup.select("#order_f > tr:nth-child({}) > td.p_num".format(i))
    str = soup.select('#order_f > tr:nth-child({}) > td:nth-child(2) > a'.format(i))
    for (s,y,z) in zip(soups,p,str):
        money.append(s.text.replace(",",""))
        rate.append(y.text.strip().replace("%",""))
        city.append(z.text)
money.pop(1)
rate.pop(1)
city.pop(1)

# 将爬取的数据封装到excel中

wb = Workbook()
ws = wb.active
ws.title = "全国城市住宅房价排行榜"
ws.append(["城市名称","平均房价","同比"])
for i,j,k in zip(city,money,rate):
    ws.append([i,j,k])
wb.save("全国城市住宅房价排行榜.xlsx")

#可视化
#房价柱状图
bar = Bar()
bar.add_xaxis(city)
bar.add_yaxis("平均房价",money,z=0)
#bar.add_yaxis("同比",num2)
bar.set_global_opts(title_opts=options.TitleOpts(title='全国房价排行'),toolbox_opts=options.ToolboxOpts())
bar.extend_axis(yaxis=options.AxisOpts(
                axislabel_opts=options.LabelOpts(formatter="{value}%"), interval=5
            ))

#房价折线图
line = Line()
line.add_xaxis(city)
line.add_yaxis("同比增长 单位:%",rate,color='black',yaxis_index=1)
line.set_global_opts(title_opts=options.TitleOpts(title='全国房价排行'))

bar.overlap(line)
bar.render("房价柱状图.html")

#房价饼状图
data = [(city[0],money[0]),(city[1],money[1]),(city[2],money[2]),(city[3],money[3]),(city[4],money[4]),
        (city[5],money[5]),(city[6],money[6]),(city[7],money[7]),(city[8],money[8]),(city[9],money[9])]
pie = Pie()
pie.add(
    series_name="平均房价",
    data_pair=data,
    radius=['30%','70%']
)
pie.set_global_opts(title_opts=options.TitleOpts(title='全国房价排行'))
pie.set_series_opts(label_opts=options.LabelOpts(formatter='{b}:{d}%'))
pie.render("房价饼状图.html")